class_name Zombie
extends CharacterBody2D

# 丧尸属性
@export var move_speed: float = 30.0
@export var attack_range: float = 40.0
@export var detection_range: float = 800
@export var attack_damage: int = 10
@export var attack_cooldown: float = 1.5
# 受击反馈属性
@export var hit_flash_duration: float = 0.2
@export var knockback_force: float = 100.0
@export var knockback_duration: float = 0.3

# 组件引用
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var state_machine: NodeStateMachine = $StateMachine
@onready var hit_component: HitComponent = $HitComponent
@onready var health_component = $HealthComponent
@onready var hurt_component = $HurtComponent

# 目标和状态变量
var target_player: Player       = null
var last_attack_time: float     = 0.0
var is_attacking: bool          = false
var is_dead: bool               = false
var is_hit_stunned: bool        = false
var hit_stun_timer: float       = 0.0
var knockback_velocity: Vector2 = Vector2.ZERO
var knockback_timer: float      = 0.0
# 受击闪烁效果
var hit_flash_timer: float = 0.0
var original_modulate: Color


func _ready():
	add_to_group("enemies")
	# 保存原始颜色
	original_modulate = animated_sprite.modulate
	# 查找玩家
	find_player()
	# 连接受伤信号
	hurt_component.hurt.connect(_on_hurt)
	health_component.died.connect(_on_died)


func _physics_process(delta):
	# 如果没有目标，尝试查找玩家
	if not target_player:
		find_player()

	# 更新攻击冷却
	last_attack_time += delta

	# 更新受击闪烁效果
	if hit_flash_timer > 0:
		hit_flash_timer -= delta
		# 闪烁效果
		var flash_alpha = sin(hit_flash_timer * 20) * 0.5 + 0.5
		animated_sprite.modulate = Color(1, flash_alpha, flash_alpha, 1)

		if hit_flash_timer <= 0:
			animated_sprite.modulate = original_modulate

	# 更新受击硬直
	if is_hit_stunned:
		hit_stun_timer -= delta
		if hit_stun_timer <= 0:
			is_hit_stunned = false

	# 更新击退效果
	if knockback_timer > 0:
		knockback_timer -= delta
		velocity = knockback_velocity
		move_and_slide()

		if knockback_timer <= 0:
			knockback_velocity = Vector2.ZERO


func find_player():
	# 查找场景中的玩家
	var players: Array[Node] = get_tree().get_nodes_in_group("player")
	if players.size() > 0:
		target_player = players[0]


func get_distance_to_player() -> float:
	if not target_player:
		return INF
	return global_position.distance_to(target_player.global_position)


func get_direction_to_player() -> Vector2:
	if not target_player:
		return Vector2.ZERO
	return (target_player.global_position - global_position).normalized()


func can_attack() -> bool:
	return last_attack_time >= attack_cooldown and not is_attacking and not is_dead


func perform_attack():
	if can_attack() and target_player:
		is_attacking = true
		last_attack_time = 0.0
		# 播放攻击动画
		animated_sprite.play("knocked")  # 使用现有的动画作为攻击动画


# 受伤处理
func _on_hurt(damage: int):
	if is_dead:
		return

	# 播放受击动画
	animated_sprite.play("knocked")

	# 受击闪烁效果
	hit_flash_timer = hit_flash_duration

	# 受击硬直
	is_hit_stunned = true
	hit_stun_timer = 0.1

	# 击退效果
	if target_player:
		var knockback_direction = (global_position - target_player.global_position).normalized()
		knockback_velocity = knockback_direction * knockback_force
		knockback_timer = knockback_duration

	print("僵尸受到伤害: ", damage)


# 死亡处理
func _on_died():
	is_dead = true
	print("僵尸死亡")
	# 切换到死亡状态
	state_machine.transition_to("die")


func _on_attack_animation_finished():
	is_attacking = false
